package com.touchwisdom.modular.common.system.user;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.touchwisdom.modular.common.dao.HibernateDao;
import com.touchwisdom.modular.util.config.Page;
import com.touchwisdom.modular.util.config.YesNoType;

@Repository
public class TUserDao extends HibernateDao<TUser, String> {

	/**
	 * 根据登录名判断数据库中是否存在这个用户
	 * 
	 * @return
	 */
	public boolean getUserByName(String name) {
		boolean flag = false;
		TUser user = this.findUnique(
				"from TUser u where u.state = ? and u.userName = ? ",
				YesNoType.Y.name(), name);
		if (user == null) {
			return flag;
		} else {
			flag = true;
		}
		return flag;
	}

	/**
	 * @author 周恪竭
	 * @param name
	 *            用户名
	 * @param password
	 *            登录名
	 * @return 根据用户名和密码获取用户,如果密码不正确返回为null
	 */
	public boolean getUserByNameMM(String name, String password) {
		boolean flag = false;
		List<TUser> list = this
				.find("from TUser u where u.userName =? and u.userPass = ? and u.state = ?",
						name, password, YesNoType.Y.name());
		if (list == null || list.size() == 0) {
			return flag;
		} else {
			flag = true;
		}
		return flag;

	}

	/**
	 * 根据用户id获取用户信息
	 * 
	 * @param userId
	 *            用户id
	 * @return
	 */
	public TUser getUserByUserId(String userId) {
		return this.get(userId);
	}

	/**
	 * 修改用户信息
	 * 
	 * @param user
	 */
	public void updateUser(TUser user) {
		Session s = this.getSession();
		s.clear();
		this.save(user);
	}

	/**
	 * @author 周恪竭 获取所有的用户信息
	 * @param column
	 *            数据库表的字段
	 * @param ReceiverId
	 *            对应字段值
	 * @return 封装好的收到信息的对象的集合
	 */
	@SuppressWarnings("unchecked")
	public List<TUser> getUserList(String hql, int pageSize, int pageNo) {
		Session s = this.getSession();
		Query query = s.createQuery(hql);
		query.setFirstResult(pageNo);
		query.setMaxResults(pageSize);
		return query.list();
	}

	/**
	 * @author 周恪竭
	 * @param user
	 *            用户对象
	 */
	public void saveUser(TUser user) {
		user.setState(YesNoType.Y.name());
		this.save(user);

	}

	/**
	 * 删除用户
	 * 
	 * @author yunjum
	 * @param id
	 * 
	 */
	public String deleteUser(String id) {
		// 逻辑删除，把它的标识位设置为0
		final String hql = "update TUser u  set u.state= :s where id =:id";
		this.getSession().createQuery(hql)
				.setParameter("s", YesNoType.N.name()).setParameter("id", id)
				.executeUpdate();
		String userName = this.get(id).getRealName();
		// this.delete(id);
		return userName;

	}

	/**
	 * @author zuzhilong
	 * @date 2013-05-06
	 * @param userName
	 *            用户名
	 * @return 根据用户名进行模糊查询分页进行查询获取所有的用户信息
	 */
	@SuppressWarnings("unchecked")
	public List<TUser> getUserListByName(String userName, String hql,
			int pageSize, int pageNo) {
		Session s = this.getSession();
		hql += "from TUser u where u.state = '" + YesNoType.Y.name() + "'";
		if (StringUtils.isNotBlank(userName)) {
			hql += " and u.userName like " + '%' + userName + '%';
		}
		Query query = s.createQuery(hql);
		query.setFirstResult(pageNo);
		query.setMaxResults(pageSize);
		return query.list();
	}

	/**
	 * 根据角色id获取所有和该角色相关联的人员
	 * 
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getInThisRoleUsers(Page<TUser> page, String roleId) {
		// 获取所有和该角色相关联的人员
		Page<TUser> yUserList = this
				.findPage(
						page,
						"from TUser u where u.id in (select t.userId from TUserRole t where t.roleId = ?) and u.state = ?",
						roleId, YesNoType.Y.name());
		return yUserList;
	}

	/**
	 * 根据角色id获取所有和该角色相关联的人员
	 * 
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getInThisRoleUsers(Page<TUser> page, String roleId,
			String roleName) {
		// 获取所有和该角色相关联的人员
		Page<TUser> yUserList = this
				.findPage(
						page,
						"from TUser u where u.id in (select t.userId from TUserRole t where t.roleId = ?) and u.realName like ? and u.state = ?",
						roleId, '%' + roleName + '%', YesNoType.Y.name());
		return yUserList;
	}

	/**
	 * 获取不在该角色下的用户们
	 * 
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getNotInThisRoleUsers(Page<TUser> page, String roleId) {
		Page<TUser> wUserList = findPage(
				page,
				"from TUser u where u.id not in(select t.userId from TUserRole t where t.roleId = ?) and u.state = ?",
				roleId, YesNoType.Y.name());
		return wUserList;
	}

	/**
	 * 根据角色id和搜索条件在角色外进行搜索
	 * 
	 * @author
	 * @author wangchao
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getNotInThisRoleUsers(Page<TUser> page, String roleId,
			String roleName) {
		Page<TUser> wUserList = findPage(
				page,
				"from TUser u where u.id not in(select t.userId from TUserRole t where t.roleId = ?) and u.realName like ? and  u.state = ? ",
				roleId, '%' + roleName + '%', YesNoType.Y.name());
		return wUserList;
	}

	/**
	 * 根据部门id得到用户列表
	 * 
	 * @param departmentId
	 * @return
	 */
	public List<TUser> getUserByDepId(String departmentId) {
		String hql = "from TUser where department.id = '" + departmentId + "'";
		return this.find(hql);
	}

	/**
	 * 查询所有未逻辑删除用户
	 * 
	 * @return
	 */
	public List<TUser> getAllByState() {
		String hql = "from TUser where state = ?";
		return this.find(hql, YesNoType.Y.name());
	}
}